#include "usr_inc.h"

#define BUF6_MAX_SIZE 16

/* 定义uart6接收体实例 */
UartRx_t rx6;
uint8_t buf6No1[BUF6_MAX_SIZE];
uint8_t buf6No2[BUF6_MAX_SIZE];
uint8_t buf6No3[BUF6_MAX_SIZE];
uint8_t buf6No4[BUF6_MAX_SIZE];

/**
 * @brief 初始化所有串口接收体实例
 */
void ALL_BSP_UART_Init(void)
{
    BSP_UART_Init(&rx6, &huart6);
    HAL_UART_Receive_IT(rx6.huart, &rx6.data, 1);
}

/**
 * @brief 重定义串口接收回调函数
 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if (huart == &huart6)
    {
        if (rx6.state == 0 && rx6.data == '@')
        {
            rx6.state = 1;
        }
        else if (rx6.state == 1 && rx6.data == '#')
        {
            rx6.state = 2;
        }
        else if (rx6.state == 2)
        {
            if (rx6.cnt >= BUF6_MAX_SIZE)
            {
                rx6.state = 0;
                rx6.cnt = 0;
            }

            if (rx6.data == '$')
            {
                buf6No1[rx6.cnt] = '\0';

                rx6.state = 3;
                rx6.cnt = 0;
            }
            else
            {
                buf6No1[(rx6.cnt)++] = rx6.data;
            }
        }
        else if (rx6.state == 3)
        {
            if (rx6.cnt >= BUF6_MAX_SIZE)
            {
                rx6.state = 0;
                rx6.cnt = 0;
            }

            if (rx6.data == '%')
            {
                buf6No2[rx6.cnt] = '\0';

                rx6.state = 4;
                rx6.cnt = 0;
            }
            else
            {
                buf6No2[(rx6.cnt)++] = rx6.data;
            }
        }
        else if (rx6.state == 4)
        {
            if (rx6.cnt >= BUF6_MAX_SIZE)
            {
                rx6.state = 0;
                rx6.cnt = 0;
            }

            if (rx6.data == '^')
            {
                buf6No3[rx6.cnt] = '\0';

                rx6.state = 5;
                rx6.cnt = 0;
            }
            else
            {
                buf6No3[(rx6.cnt)++] = rx6.data;
            }
        }
        else if (rx6.state == 5)
        {
            if (rx6.cnt >= BUF6_MAX_SIZE)
            {
                rx6.state = 0;
                rx6.cnt = 0;
            }

            if (rx6.data == '&')
            {
                buf6No4[rx6.cnt] = '\0';

                PidArgsMsg_t args = {0};
                args.target = atof(buf6No1);
                args.kp = atof(buf6No2);
                args.ki = atof(buf6No3);
                args.kd = atof(buf6No4);
                xQueueSendToBackFromISR(pidArgsQueueHandle, &args, NULL);

                rx6.state = 0;
                rx6.cnt = 0;
            }
            else
            {
                buf6No4[(rx6.cnt)++] = rx6.data;
            }
        }

        HAL_UART_Receive_IT(rx6.huart, &rx6.data, 1);
    }
}

/**
 * @brief 重定向printf到USART6
 */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
    HAL_UART_Transmit(&huart6, (uint8_t *)&ch, 1, 0xFFFF);
    return ch;
}
